(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:CashreqCtrl
     * @description
     * # CashreqCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('CashreqCtrl', CashreqCtrl);

    CashreqCtrl.$inject = ['$scope', 'dataService', '$location', '$state', 'helper'];

    function CashreqCtrl($scope, dataService, $location, $state, helper) {
        //显示
        $scope.listAction = function() {
            // $scope.ordersTotal = 0;
            $scope.pageSize = 10;
            $scope.getcashreqsList(1);
        }
        
        //获取列表
        $scope.getcashreqsList = function(pageNo) {
            var query = { page_no: pageNo ? pageNo : "1", page_size: $scope.pageSize };
            if ($scope.tid) {
                query.query = { "tid": $scope.tid };
            }
            dataService.cashreqGet(query).then(function(list) {
                if (list) {
                    $scope.cashreqslist = list.cash_reqs;
                    $scope.cashreqTotal = list.total_count;
                }
            });
        }
        
        //添加数据 初始化
        $scope.addAction = function() {
            $scope.cashreq = {};
            $scope.parentCat = {};
            var id = $location.search().id;
            if (id) {
                dataService.cashreqOne(id).then(function(list) {
                    if (list) {
                        $scope.cashreq = list;
                    }
                });
            } else {
                //初始化值
                
            }
        }

        // 退入商品明细
        function cashreq_items() {
            // var return_item_id = "S00" + random(11111, 99999);
            return {
                return_item_id: "1100" + random(11111, 99999),
                return_form_id: "T00" + random(11111, 99999),
                item_id: 100,
                sku_id: 1, //条码
                num: 22,
                price: 110,
                discount_fee: 22,
                payment: 2200,
                remark: '',
            };
        }
        $scope.setOrder = function(order) {
            // console.log("112312312", order);
            if (order) {
                $scope.cashreq.tid = order.order_id;
                $scope.cashreq.in_items = clone(order.items);
                $scope.cashreq.out_items = clone(order.items);
            }
        }
        $scope.orderAdd = function() {

            if (!$scope.cashreq.cash_req_id) {
                dataService.cashreqAdd($scope.cashreq).then(function(re) {
                    if (re.cash_req_id) {
                        helper.myAlert("success", null, null, function() {
                            $state.go("cashreq.list");
                        });
                    } else {
                        helper.myAlert("error " + re.Msg);
                    }
                });
            } else {
                dataService.cashreqUpdate($scope.cashreq).then(function(re) {
                    if (re.cash_req_id) {
                        helper.myAlert("success", null, null, function() {
                            $state.go("cashreq.list");
                        });
                    } else {
                        helper.myAlert("error " + re.Msg);
                    }
                });
            }
        }

        //显示
        $scope.infoAction = function() {
            var id = $location.search().id;
            if (id) {
                dataService.cashreqOne(id).then(function(list) {
                    if (list) {
                        $scope.cashreq = list;
                    } else {
                        helper.myAlert("No data found", null, null, function() {
                            $state.go("cashreq.list");
                        });
                    }
                });
            } else {
                helper.myAlert("No data found", null, null, function() {
                    $state.go("cashreq.list");
                });
            }
        }
        $scope.statusUpdata = function(id, type) {
            if (id) {
                var data = { return_form_id: id, event: "R_EVENT_REVIEW_REQ" };
                dataService.cashreqStatusUpdate(data).then(function(list) {
                    if (list.return_form_id) {
                        helper.myAlert("success");
                    } else {
                        helper.myAlert("No data found");
                    }
                });
            }
        }
    }
})();
